<template>
  <div class="cinema-container">
    <div class="cinema-header">
      <div class="cinema-blank" style="border-top-left-radius: 45px">WC</div>
      <div class="seat cinemaNum">{{ cinemaNum }}</div>
      <div class="cinema-blank" style="border-top-right-radius: 45px">WC</div>
    </div>
    <div class="cinema-seating">
      <div v-for="(row, rowIndex) in rows" :key="rowIndex" class="cinema-row">
        <div
          v-for="(seat, seatIndex) in row"
          :key="seatIndex"
          :class="[
            'seat',
            {
              aisle: seat.type === 'aisle',
              purchased: seat.status === 'purchased',
              selected: seat.status === 'selected' && !seat.userSelected,
              'user-selected': seat.userSelected,
            },
          ]"
          @click="handleSeatClick(seat)"
        >
          {{ seat.type !== "aisle" ? seat.label : "" }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    rows: {
      type: Array,
      required: true,
    },
    cinemaNum: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      selectedSeats: [], // 用于记录用户刚刚选择的座位
    };
  },
  methods: {
    handleSeatClick(seat) {
      if (seat.type === "aisle" || seat.status === "purchased") return;

      if (seat.userSelected) {
        // 取消选择
        seat.userSelected = false;
        seat.status = "available";
        this.selectedSeats = this.selectedSeats.filter((s) => s !== seat);
      } else if (seat.status === "available") {
        // 选择座位
        seat.userSelected = true;
        seat.status = "selected";
        this.selectedSeats.push(seat);
      }
    },
    getSeats() {
      return {
        seats: this.selectedSeats,
        cinemaNum: this.cinemaNum,
      };
    },
  },
};
</script>

<style scoped>
.cinema-container {
  background: black;
  width: 85%;
  height: 0;
  flex: 1;
  padding: 5px;
  display: flex;
  flex-direction: column;
  border: 1px solid #fff;
  border-radius: 5px;
  margin: 5px;
}
.cinema-header {
  display: flex;
  justify-content: center;
  margin-bottom: 5px;
  height: 20px;
}
.cinema-seating {
  flex: 1;
  height: 0;
  overflow-y: scroll;
}
.cinema-row {
  display: flex;
  justify-content: center;
}
.cinema-blank,
.seat {
  width: 50px;
  height: 15px;
  margin: 2px 2px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 4px;
  cursor: pointer;
  border: 0.5px solid #f44336;
  color: #f44336;
}
.cinema-blank {
  width: 100px;
  border-color: #9e9e9e;
  color: #9e9e9e;
}
.cinemaNum {
  border: none;
  color: #fff;
}
.seat.purchased {
  background-color: #f44336; /* 已购买座位颜色 */
  border-color: #f44336;
  color: white;
}
.seat.selected {
  background-color: #000; /* 已选择座位颜色 */
  border-color: #9e9e9e; /* 已选择座位颜色 */
  color: #9e9e9e;
}
.seat.user-selected {
  background-color: #ffffff; /* 用户选中座位颜色 */
  color: #f44336;
  border-color: #f44336;
}
.aisle {
  visibility: hidden;
}
</style>
